var mb_parser = {
  getChannel: function(){
    if (/ads/.test(window.location.href)) {
      return 'LolkingTest';
    }

    var channel = 'LolkingOther';
    var uri = window.location.pathname;
    if (uri.indexOf('/models') == 0) channel = 'LolkingModel';
    else if (uri.indexOf('/summoner') == 0) channel = 'LolkingSummoner';
    else if (uri.indexOf('/champions') == 0) channel = 'LolkingChampion';
    else if (uri.indexOf('/search') == 0) channel = 'LolkingSearch';
    else if (uri.indexOf('/charts') == 0) channel = 'LolkingChart';
    else if (uri.indexOf('/streams') == 0) channel = 'LolkingStream';
    else if (uri.indexOf('/guides') == 0) channel = 'LolkingGuides';
    else if (uri == '/') channel = 'LolkingHome';
    return channel;
  },
  getSiteId: function(config) {
    if (typeof(I11C) !== "undefined" && I11C.Morph == 1) {
      return config.instart_site_id;
    } else {
      return config.normal_site_id;
    }
  },
  checkScreenSize: function(slot, current_inner_width){
    if(window.innerWidth < current_inner_width || slot.min_width < current_inner_width) return false;
    return true;
  },
  allowSlot: function(slot, type, channel, current_inner_width){
    if(!slot) return false;
    if(slot.only_channels && slot.only_channels[type] && slot.only_channels[type].indexOf(channel) == -1) return false;
    if(slot.remove_channels && slot.remove_channels[type] && slot.remove_channels[type].indexOf(channel) > -1) return false;
    return true;
  },
  addATFSlot: function(config, channel){
    //Will change to be multi-sized
    if(config.adslots.atf){
      if (this.checkScreenSize(config.adslots.atf, 970)) {
        if(this.allowSlot(config.adslots.atf, "large", channel, 970)) {
          MonkeyBroker.addSlot({slot:'horizontal-atf', sizes: config.adslots.atf.large, el: "za-leaderboard" });
        }
      } else if (this.checkScreenSize(config.adslots.atf, 768)) {
        if (this.allowSlot(config.adslots.atf, "medium", channel, 768)) {
          MonkeyBroker.addSlot({slot:'horizontal-atf', sizes: config.adslots.atf.medium, el: "za-leaderboard" });
        }
      } else if (this.checkScreenSize(config.adslots.atf, 0)) {
        if(this.allowSlot(config.adslots.atf, "small", channel, 0)) {
          MonkeyBroker.addSlot({slot:'horizontal-atf', sizes: config.adslots.atf.small, el: "za-leaderboard" });
        }
      }
    }
  },
  addBTFSlot: function(config, channel){
    if(config.adslots.btf){
      if (this.checkScreenSize(config.adslots.btf, 768)) {
        if (this.allowSlot(config.adslots.btf, "medium", channel, 768)) {
          MonkeyBroker.addSlot({slot:'horizontal-btf', sizes: config.adslots.btf.medium, el:"bottom-leaderboard" });
        }
      } else if (this.checkScreenSize(config.adslots.btf, 0)) {
        if(this.allowSlot(config.adslots.btf, "small", channel, 0)) {
          MonkeyBroker.addSlot({slot:'horizontal-btf', sizes: config.adslots.btf.small, el:"bottom-leaderboard"});
        }
      }
    }
  },
  addATFVerticalSlot: function(config, channel){
    if(config.adslots.atf_vertical){
      if (this.checkScreenSize(config.adslots.atf_vertical, 1024)) {
        if (this.allowSlot(config.adslots.atf_vertical, "large", channel, 1024)) {
          MonkeyBroker.addSlot({slot:'vertical-atf', sizes: config.adslots.atf_vertical.large, el: "za-medrec-atf" });
        }
      } else if (this.checkScreenSize(config.adslots.atf_vertical, 768)) {
        if (this.allowSlot(config.adslots.atf_vertical, "medium", channel, 768)) {
          MonkeyBroker.addSlot({slot:'vertical-mobile-atf', sizes: config.adslots.atf_vertical.medium, el: "za-medrec-atf-mob" });
        }
      } else if (this.checkScreenSize(config.adslots.atf_vertical, 0)) {
        if(this.allowSlot(config.adslots.atf_vertical, "small", channel, 0)){
          MonkeyBroker.addSlot({slot:'vertical-mobile-atf', sizes: config.adslots.atf_vertical.small, el: "za-medrec-atf-mob" });
        }
      }
    }
  },
  addBTFVerticalSlot: function(config, channel){
    if(config.adslots.btf_vertical){
      if (this.checkScreenSize(config.adslots.btf_vertical, 1024)) {
        if (this.allowSlot(config.adslots.btf_vertical, "large", channel, 1024)) {
          MonkeyBroker.addSlot({slot:'vertical-btf', sizes: config.adslots.btf_vertical.large, el: "za-medrec-btf" });
        }
      } else if (this.checkScreenSize(config.adslots.btf_vertical, 768)) {
        if (this.allowSlot(config.adslots.btf_vertical, "medium", channel, 768)) {
          MonkeyBroker.addSlot({slot:'vertical-mobile-btf', sizes: config.adslots.btf_vertical.medium, el: "za-medrec-btf-mob" });
        }
      } else if (this.checkScreenSize(config.adslots.btf_vertical, 0)) {
        if(this.allowSlot(config.adslots.btf_vertical, "small", channel, 0)) {
          MonkeyBroker.addSlot({slot:'vertical-mobile-btf', sizes: config.adslots.btf_vertical.small, el: "za-medrec-btf-mob" });
        }
      }
    }
  },
  setTO(id){
    var self = this;
    setTimeout(function(){
      if(document.getElementById(id) != null){
        googletag.cmd.push(function() { googletag.display(id); });
      }
      else {
        console.log(id, "is null");
        self.setTO(id);
      }
    }, 300);
  },
  parse: function(config) {
    if (typeof MonkeyBroker !== "undefined") {
      MonkeyBroker.site = {"id": this.getSiteId(config),
      "customDomainName": 'mb.zam.com',
      "reportBadAd": true,
      "reportBadAdText": "Report Ad",
      "noSandbox": false,
      "lightboxCallback": function (e, t) {
        var iframeAdReport = document.createElement("iframe");
        iframeAdReport.src = e;
        iframeAdReport.height = 0;
        iframeAdReport.width = 0;
        document.getElementsByTagName("body")[0].appendChild(iframeAdReport);

        t.parentNode.removeChild(t);

        alert("Thank you for your feedback, we will look into it!");
      }
    };
    var channel = this.getChannel();
    MonkeyBroker.addAttribute("mb_pc", channel);
    MonkeyBroker.addAttribute("mb_ri", config.refresh_rate);
    this.addATFSlot(config, channel);
    this.addBTFSlot(config, channel);
    this.addATFVerticalSlot(config, channel);
    this.addBTFVerticalSlot(config, channel);
    for(var i in config.adslots.additional_slots){
      MonkeyBroker.addSlot(config.adslots.additional_slots[i]);
    }

    MonkeyBroker.registerSlotCallback('horizontal-atf', function(ad){
      var adWidth = parseInt(ad.w);
      var adHeight = parseInt(ad.h);
      $('body').removeClass('has-adblocker');
      $('body').removeClass('has-970x90');
      $('body').removeClass('has-970x250');
      $('body').removeClass('has-728x90');

      if (adWidth === 970) {
        $('body').toggleClass('has-970x90', (adHeight === 90));
        $('body').toggleClass('has-970x250', (adHeight === 250));
      }
      if (adWidth === 728 && window.innerWidth > 1280) {
        $('body').toggleClass('has-728x90', (adHeight === 90));
      }
      $( document ).ready(function() {
        if (typeof readjustColumns === "function") readjustColumns();
      });
    });

    MonkeyBroker.registerSizeCallback(30, function(skin){
      if (!handledSkin) {
        handledSkin = true;

        // Start original ZAF stuff
        var target = document.getElementById('skin-wrap');
        if (target) {
          target.innerHTML = '';
        } else {
          target = document.body;
        }

        // Add class to the body to show the page is skinned
        document.body.className += ' skinned' +(skin.usePageLeaderboard ? ' ' + 'skinned-use-page-leaderboard' : '');

        // Add links
        var linkClassPrefix = 'skin-link-';
        var i = 0;
        var skinLinkCount = 1;
        while (i++ < skinLinkCount) {
          var a = target.insertBefore(
            document.createElement('a'),
            target.firstChild
          );
          a.className = linkClassPrefix + i;
          a.target = '_blank';
          a.href = skin.link;
          a.style.backgroundImage = 'url(' + skin.image + ')';
        }

        // Add client tracking pixel(s) if there are any
        var addImpressionTracker = function(target, url) {
          var img = target.insertBefore(
            document.createElement('img'),
            target.firstChild
          );
          img.className = 'skin-pixel';
          img.src = url;
        };
        switch (typeof skin.impressionTracking) {
          case 'string':
          addImpressionTracker(target, skin.impressionTracking);
          break;
          case 'object':
          for (var j in skin.impressionTracking) {
            if (skin.impressionTracking.hasOwnProperty(j) && typeof skin.impressionTracking[j] == 'string') {
              addImpressionTracker(target, skin.impressionTracking[j]);
            }
          }
          break;
        }
      }
    });

    MonkeyBroker.go();
  }
}
}
